package com.lk.org.service.impl;

import com.lk.basic.common.MailCommonService;
import com.lk.basic.constans.BaseConstants;
import com.lk.basic.service.IBaiduAuditService;
import com.lk.basic.util.AjaxResult;
import com.lk.org.domain.Employee;
import com.lk.org.domain.Shop;
import com.lk.org.domain.ShopAuditLog;
import com.lk.org.mapper.EmployeeMapper;
import com.lk.org.mapper.ShopAuditLogMapper;
import com.lk.org.mapper.ShopMapper;
import com.lk.org.service.IShopService;
import com.lk.basic.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lk
 * @since 2022-11-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IBaiduAuditService baiduAuditService;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private MailCommonService mailCommonService;

    @Transactional
    @Override
    public void settlement(Shop shop) throws Exception{
        Shop shopByDb=shopMapper.loadByName(shop.getName());
        validate(shop,shopByDb);
        if (Objects.nonNull(shopByDb) && shopByDb.getState().equals(BaseConstants.Shop.STATE_REJECT_AUDIT)){
            shop.setId(shopByDb.getId());
        }
        shop.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shop.setRegisterTime(new Date());//STATE_REJECT_AUDIT
        if (shop.getId()!=null){
            shopMapper.update(shop);
        }else {
            shopMapper.insert(shop);
        }
        Employee admin = shop.getAdmin();
        Employee adminByDb=employeeMapper.loadByUsername(admin.getUsername());

        if(Objects.nonNull(adminByDb)){
            shop.setAdminId(adminByDb.getId());
        }else{
            // 如果是第一次新增管理员信息,在店铺未激活的时候,店铺管理员也不能使用,状态=待激活
            employeeMapper.insert(admin);
            shop.setAdminId(admin.getId());
        }

        String text=shop.getName()+""+shop.getAddress();
        AjaxResult ajaxResult = baiduAuditService.audit(text, null, null);
        if (ajaxResult.getSuccess()){
            shop.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
            mailCommonService.test2("店铺激活通知","<a href='http://localhost:80/shop/active2/"+shop.getId() +"'>激活链接</a>",admin.getEmail());
            }else {
            shop.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
            mailCommonService.sendSimpleMail("店铺激活失败告知","由于种种原因，店铺入驻失败",admin.getEmail());

        }
        shopMapper.update(shop);

        ShopAuditLog log = new ShopAuditLog();
        log.setState(ajaxResult.getSuccess()?1:0);
        log.setShopId(shop.getId());
        log.setAuditTime(new Date());
        log.setNote(ajaxResult.getSuccess()?"审核通过":"被驳回了，原因:"+ajaxResult.getMessage());
        shopAuditLogMapper.insert(log);
    }

    @Override
    public void active(Long id) {
        System.out.println("激活成功");
        shopMapper.active(id);
    }

    @Override
    public void examine(Shop shop) {
        shopMapper.examine(shop);
    }


    private void validate(Shop shop,Shop shopByDb) {
        if (Objects.isNull(shop)){
            throw new RuntimeException("系统错误！请检查参数后重新提交");
        }
        if (StringUtils.isEmpty(shop.getName())||StringUtils.isEmpty(shop.getTel())||StringUtils.isEmpty(shop.getAddress())){
            throw new RuntimeException("系统错误，请检查后重新提交");
        }
        Employee admin= shop.getAdmin();
        if (Objects.isNull(admin)){
            throw new RuntimeException("系统错误，请检查参数后重新提交");
        }
        if (StringUtils.isEmpty(admin.getUsername())||StringUtils.isEmpty(admin.getPassword())||
                StringUtils.isEmpty(admin.getPhone())||StringUtils.isEmpty(admin.getEmail())||StringUtils.isEmpty(admin.getConfirmPassword())){
            throw new RuntimeException("系统错误，请检查参数后重新提交");
        }
        if (Objects.nonNull(shopByDb) && !shopByDb.getState().equals(BaseConstants.Shop.STATE_REJECT_AUDIT)){
            throw new RuntimeException("店铺已存在，禁止重复填入");
        }
        if (!admin.getPassword().equals(admin.getConfirmPassword())){
            throw new RuntimeException("两次密码不一致，请重新填写");
        }
    }



}
